<?php
/**
 * Created by : PhpStorm
 * Index: 阿城 <479468212@qq.com>
 * Date: 2019/12/27
 * Time: 13:02
 */

namespace app\api\controller\article;

use app\api\controller\Base;
use app\common\model\CommonArticle as articleModel;
use app\common\model\CommonArticleCategory as categoryModel;
use think\facade\Db;
use app\util\Tools;


class Index extends Base
{
    /**
     * @OA\Get(
     *   path="/api/article/index",
     *   tags={"文章相关"},
     *   summary="文章列表",
     *   @OA\Parameter(ref="#/components/parameters/AppClientVersion"),
     *   @OA\Parameter(ref="#/components/parameters/AppDeviceType"),
     *   @OA\Parameter(ref="#/components/parameters/keywords"),
     *   @OA\Parameter(name="category_id",in="query",description="分类ID，不传取全部数据",@OA\Schema( type="integer", format="int32", minimum=1.0 )),
     *   @OA\Parameter(ref="#/components/parameters/order"),
     *   @OA\Parameter(ref="#/components/parameters/size"),
     *   @OA\Parameter(ref="#/components/parameters/page"),
     *   @OA\Response(
     *     response="200",
     *     description="返回JSON",
     *     @OA\MediaType(
     *      mediaType="application/json",
     *      @OA\Schema(
     *         @OA\Property(property="code", type="number",description="状态码"),
     *         @OA\Property(property="msg", type="string",description="提示消息"),
     *         @OA\Property(property="data", type="object",description="数据列表",@OA\Items),
     *      )
     *    )
     *   ),
     *   @OA\Response(response="500", description="服务器错误"),
     *   security={{"App-Login-Token": ""}}
     * )
     */
    public function index()
    {
        $limit       = $this->request->get('size', config('easyApi.ADMIN_LIST_DEFAULT'));
        $start       = $this->request->get('page', 1);
        $keywords    = $this->request->get('keywords');
        $order       = $this->request->get('order','top DESC,id DESC');
        $category_id = $this->request->get('category_id');
   
        //取文章
        $articleModel = new articleModel();
        if(!empty($category_id)){
           $articleIdArr = Db::name('common_article_category_rel')->where(['category_id'=>$category_id])->column('article_id');
           $articleModel = $articleModel->where('id','in',$articleIdArr);
        }
        if (!empty($keywords)) {
            $articleModel = $articleModel->where('title like "%'.$keywords.'%"');
        }
        $listObj = $articleModel->order($order)->paginate($limit, false, ['page' => $start])->toArray();
        $this->success('获取文章成功', [
            'list'  => $listObj['data'],
            'count' => $listObj['total']
        ]);
    }
}
